"""
连接配置模型 - PyQt5版本
管理连接相关的配置参数和状态，使用Qt信号机制
"""
from PyQt5.QtCore import QObject, pyqtSignal
from typing import List
from dataclasses import dataclass

@dataclass
class ConnectionConfig:
    """连接配置数据类"""
    com_port: str = ""          # COM口
    tx_can_id: str = "1"        # 发送CAN ID（10进制，默认1）
    rx_can_id: str = "0"        # 接收CAN ID（固定为0）
    baudrate: int = 500000      # 波特率（默认500000）
    is_connected: bool = False  # 连接状态

class ConnectionModel(QObject):
    """连接模型类 - 基于QObject，支持信号发射"""
    
    # 定义信号
    status_changed = pyqtSignal(bool)                    # 连接状态变化信号
    config_changed = pyqtSignal(ConnectionConfig)       # 配置变化信号
    ports_updated = pyqtSignal(list)                    # COM口列表更新信号
    
    def __init__(self, parent=None):
        """初始化连接模型
        
        Args:
            parent: 父对象
        """
        super().__init__(parent)
        self._config = ConnectionConfig()
        self._available_ports: List[str] = []
    
    def get_config(self) -> ConnectionConfig:
        """获取当前配置
        
        Returns:
            ConnectionConfig: 当前连接配置
        """
        return self._config
    
    def set_com_port(self, port: str) -> None:
        """设置COM口
        
        Args:
            port: COM口名称
        """
        if self._config.com_port != port:
            self._config.com_port = port
            self.config_changed.emit(self._config)
    
    def set_tx_can_id(self, can_id: str) -> None:
        """设置发送CAN ID
        
        Args:
            can_id: CAN ID字符串
        """
        if self._config.tx_can_id != can_id:
            self._config.tx_can_id = can_id
            self.config_changed.emit(self._config)
    
    def set_rx_can_id(self, can_id: str) -> None:
        """设置接收CAN ID
        
        Args:
            can_id: CAN ID字符串
        """
        if self._config.rx_can_id != can_id:
            self._config.rx_can_id = can_id
            self.config_changed.emit(self._config)
    
    def set_baudrate(self, baudrate: int) -> None:
        """设置波特率
        
        Args:
            baudrate: 波特率值
        """
        if self._config.baudrate != baudrate:
            self._config.baudrate = baudrate
            self.config_changed.emit(self._config)
    
    def set_connection_status(self, connected: bool) -> None:
        """设置连接状态
        
        Args:
            connected: 连接状态
        """
        if self._config.is_connected != connected:
            self._config.is_connected = connected
            self.status_changed.emit(connected)
    
    def get_available_ports(self) -> List[str]:
        """获取可用COM口列表
        
        Returns:
            List[str]: COM口列表
        """
        return self._available_ports.copy()
    
    def set_available_ports(self, ports: List[str]) -> None:
        """设置可用COM口列表
        
        Args:
            ports: COM口列表
        """
        if self._available_ports != ports:
            self._available_ports = ports.copy()
            self.ports_updated.emit(self._available_ports)
    
    def is_valid_config(self) -> bool:
        """检查配置是否有效
        
        Returns:
            bool: 配置有效性
        """
        return (
            bool(self._config.com_port) and
            bool(self._config.tx_can_id) and
            bool(self._config.rx_can_id) and
            self._config.baudrate > 0
        )